package com.hs.archive_app

import android.app.Activity
import android.app.Application
import android.content.Context
import android.os.Bundle
import androidx.multidex.MultiDex
import com.hs.framework.helper.HsAppHelper
import com.hs.framework.log.LogUtil
import com.hs.framework.manager.ActivityManager
import com.hs.framework.manager.AppFrontBack
import com.hs.framework.manager.AppFrontBackListener
import com.hs.framework.toast.TipsToast
import com.hs.framework.utils.AppExecutors
import com.hs.framework.utils.MMKVUtil
import com.hs.framework.utils.Speech
import com.hs.serial.SerialPortManager
import com.hs.stater.dispatcher.TaskDispatcher
import dagger.hilt.android.HiltAndroidApp

@HiltAndroidApp
class App : Application() {

    override fun attachBaseContext(base: Context?) {
        super.attachBaseContext(base)
        AppExecutors.cpuIO.execute {
            MultiDex.install(base)
        }
    }

    override fun onCreate() {
        super.onCreate()
        //注册APP前后台切换监听
        appFrontBackRegister()
        // App启动立即注册监听
        registerActivityLifecycle()
        TipsToast.init(this)
        Speech.init(this)
        //1.启动器：TaskDispatcher初始化
        TaskDispatcher.init(this)
        //2.创建dispatcher实例
        val dispatcher: TaskDispatcher = TaskDispatcher.createInstance()
        var start: Long = System.currentTimeMillis()
        //3.添加任务并且启动任务
        dispatcher.addTask(InitSumHelperTask(this))
            .addTask(InitMmkvTask())
            .addTask(InitAppManagerTask())
//                .addTask(InitRefreshLayoutTask())
            .addTask(InitArouterTask())
            .start()

        //4.等待，需要等待的方法执行完才可以往下执行
        dispatcher.await()
        val path: String = MMKVUtil.getString("serialInterface", "/dev/ttyS7")
//        if (path.isEmpty()) {
//            Speech.speak("串口设备不存在 请前往配置界面配置串口设备")
//            TipsToast.showTips("请前往配置界面配置串口设备")
//        }else{
//           serial= SerialPortManager().apply {
//                initSerialPort(path)
//            }
//        }
        println("-------HSApplication---end2 ARouter-----启动耗时---${System.currentTimeMillis() - start} ms--")

    }

    /**
     * 注册APP前后台切换监听
     */
    private fun appFrontBackRegister() {
        AppFrontBack.register(this, object : AppFrontBackListener {
            override fun onBack(activity: Activity?) {
                LogUtil.d("onBack")
            }

            override fun onFront(activity: Activity?) {
                LogUtil.d("onFront")
            }
        })
    }

    /**
     * 注册Activity生命周期监听
     */
    private fun registerActivityLifecycle() {
        registerActivityLifecycleCallbacks(object : ActivityLifecycleCallbacks {
            override fun onActivityPaused(activity: Activity) {
            }

            override fun onActivityStarted(activity: Activity) {
            }

            override fun onActivityDestroyed(activity: Activity) {
                ActivityManager.pop(activity)
            }

            override fun onActivitySaveInstanceState(activity: Activity, p1: Bundle) {
            }


            override fun onActivityStopped(activity: Activity) {
            }

            override fun onActivityCreated(activity: Activity, p1: Bundle?) {
                ActivityManager.push(activity)
            }

            override fun onActivityResumed(activity: Activity) {

            }
        })
    }
}